package com.pot.admin.config.security;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pot.admin.service.IUserService;
import com.pot.common.entity.system.SystemAuthAdmin;
import com.pot.common.entity.user.User;
import com.pot.common.mapper.system.SystemAuthAdminMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;

@Component
public class CustomerUserDetailsService implements UserDetailsService {

    @Autowired
    SystemAuthAdminMapper systemAuthAdminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SystemAuthAdmin systemAuthAdmin = systemAuthAdminMapper.selectOne(Wrappers
                .<SystemAuthAdmin>lambdaQuery()
                .eq(SystemAuthAdmin::getUsername, username));

        if(systemAuthAdmin == null){
            throw new UsernameNotFoundException(String.format("%s not found " , username));
        }

        UserInfoData userInfoData = new UserInfoData();
        BeanUtils.copyProperties(systemAuthAdmin,userInfoData);

        userInfoData.setAuthorities(new ArrayList<>());

        return userInfoData;
    }
}
